﻿/// <reference path="../scripts/jquery-1.11.2.min.js" />
/// <reference path="../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    CheckUser();
    $(document).ready(onReady);
}

function onReady() {
    if ($.query.get("cid") != '') {
        $("#tit").html('车辆信息');
        var data = { id: $.query.get("cid") };
        $.ajax({
            type: "get",
            url: "api/member.ashx?ac=carInfo&k=" + Math.random(),
            data: data,
            beforeSend: function () {
                $('#loadingToast').show();
            },
            error: function () {
                showToast("获取车辆信息失败");
            },
            success: function (data) {
                if (data != null && data != '') {
                    var m = eval("(" + data + ")");
                    $("#btnDel").removeClass('hidden');
                    $("#btnSave").removeClass('hidden');
                    if ($.query.get("ctid") != '' && $.query.get("ct") != '') {
                        $("#cartype").html($.query.get("ct"));
                        $("#ctid").val($.query.get("ctid"));
                    }
                    else {
                        $("#cartype").html(m.CarType);
                        $("#ctid").val(m.CarTypeID);
                    }
                    var carNum = m.CarNumber;
                    if (carNum != '') {
                        var sim = carNum.substring(0, 1);
                        var num = carNum.substring(1);
                        $("#carSimple").html(sim);
                        $("#txtCarNumber").val(num);
                    }
                    $("#txtColor").val(m.Color);
                }
            },
            complete: function () {
                $('#loadingToast').hide();
            }
        });
    }
    else {
        if ($.query.get("ctid") != '' && $.query.get("ct") != '') {
            $("#cartype").html($.query.get("ct"));
            $("#ctid").val($.query.get("ctid"));
        }
        $("#btnAdd").removeClass('hidden');
    }

    $("#divtype").click(function () {
        var cid = '';
        if ($.query.get("cid") != '') {
            cid = '&cid=' + $.query.get("cid");
        }
        window.location.href = 'cartype2.html?url=' + jQuery.url.attr("pageurl") + cid;
    });

    //选择车牌简称
    $("#carSimple").click(function () {
        var container = $('#sim_container');
        var weuiActionsheet = $('#sim_actionsheet');
        var mask = $('#sim_mask');

        container.height("100%");
        weuiActionsheet.addClass('weui_actionsheet_toggle');
        mask.show().addClass('weui_fade_toggle').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        $('#sim_cancel').click(function () {
            hideActionSheet(container, weuiActionsheet, mask);
        });
        weuiActionsheet.unbind('transitionend').unbind('webkitTransitionEnd');
    });
    //确定车牌简称
    $("#sim-ul").children("li").click(function () {
        $("#carSimple").html($(this).html());
        var container = $('#sim_container');
        var weuiActionsheet = $('#sim_actionsheet');
        var mask = $('#sim_mask');
        hideActionSheet(container, weuiActionsheet, mask);
    });

    //添加
    $("#btnAdd").click(function () {
        var ctid = $("#ctid").val();
        var carnmber = $("#txtCarNumber").val();
        var color = $("#txtColor").val()

        if (ctid == '0' || ctid == '') {
            showToast('请选择车型');
        }
        else if (carnmber == '') {
            showToast('请填写车牌号');
        }
        else if (color == '') {
            showToast('请填写车身颜色');
        }
        else {
            var data = { ctid: ctid, carnmber: $("#carSimple").html() + carnmber, color: color };
            $.ajax({
                type: "post",
                data: data,
                url: "api/member.ashx?ac=addCar&k=" + Math.random(),
                beforeSend: function () {
                    $('#loadingToast').show();
                },
                error: function () {
                    showToast("数据提交失败");
                },
                success: function (data) {
                    var obj = eval("(" + data + ")").result;
                    if (obj.r == "suc") {
                        window.location.href = "mycar.html";
                    }
                    else {
                        showToast(obj.val);
                    }
                },
                complete: function () {
                    $('#loadingToast').hide();
                }
            });
        }
    });
    //保存
    $("#btnSave").click(function () {
        var id = $.query.get("cid");
        var ctid = $("#ctid").val();
        var carnmber = $("#txtCarNumber").val();
        var color = $("#txtColor").val()

        if (ctid == '0' || ctid == '') {
            showToast('请选择车型');
        }
        else if (carnmber == '') {
            showToast('请填写车牌号');
        }
        else if (color == '') {
            showToast('请填写车身颜色');
        }
        else {
            var data = { id: id, ctid: ctid, carnmber: $("#carSimple").html() + carnmber, color: color };
            $.ajax({
                type: "post",
                data: data,
                url: "api/member.ashx?ac=upCar&k=" + Math.random(),
                beforeSend: function () {
                    $('#loadingToast').show();
                },
                error: function () {
                    showToast("数据提交失败");
                },
                success: function (data) {
                    var obj = eval("(" + data + ")").result;
                    if (obj.r == "suc") {
                        window.location.href = "mycar.html";
                    }
                    else {
                        showToast(obj.val);
                    }
                },
                complete: function () {
                    $('#loadingToast').hide();
                }
            });
        }
    });
    //删除
    $("#btnDel").click(function () {
        var id = $.query.get("cid");
        var data = { id: id };
        $.ajax({
            type: "post",
            data: data,
            url: "api/member.ashx?ac=delCar&k=" + Math.random(),
            beforeSend: function () {
                $('#loadingToast').show();
            },
            error: function () {
                showToast("数据提交失败");
            },
            success: function (data) {
                var obj = eval("(" + data + ")").result;
                if (obj.r == "suc") {
                    window.location.href = "mycar.html";
                }
                else {
                    showToast(obj.val);
                }
            },
            complete: function () {
                $('#loadingToast').hide();
            }
        });
    });
}

function hideActionSheet(container, weuiActionsheet, mask) {
    container.height("0");
    weuiActionsheet.removeClass('weui_actionsheet_toggle');
    mask.removeClass('weui_fade_toggle');
    weuiActionsheet.on('transitionend', function () {
        mask.hide();
    }).on('webkitTransitionEnd', function () {
        mask.hide();
    })
}

